﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Windows.Forms.Design;

    internal class ColumnTemplateSettingsEditor : UITypeEditor
    {
        private IWindowsFormsEditorService editorService;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }
            if (this.editorService != null)
            {
                Column instance = (Column) context.Instance;
                Vdc.Grid.Grid gridContainer = instance.GridContainer;
                string[] templatesIds = null;
                if (gridContainer != null)
                {
                    templatesIds = gridContainer.GetTemplatesIds();
                }
                ColumnTemplateSettingsSetupControl dialog = new ColumnTemplateSettingsSetupControl((ColumnTemplateSettings) value, this.editorService, templatesIds);
                this.editorService.ShowDialog(dialog);
                if (dialog.TemplateSettings != null)
                {
                    return new ColumnTemplateSettings { CancelTemporaryAddedRecord_TemplateId = dialog.TemplateSettings.CancelTemporaryAddedRecord_TemplateId, CancelTemporaryEditedRecord_TemplateId = dialog.TemplateSettings.CancelTemporaryEditedRecord_TemplateId, EditTemplateId = dialog.TemplateSettings.EditTemplateId, FilterTemplateId = dialog.TemplateSettings.FilterTemplateId, GroupHeaderTemplateId = dialog.TemplateSettings.GroupHeaderTemplateId, HeaderTemplateId = dialog.TemplateSettings.HeaderTemplateId, ModifyTemporaryAddedRecord_TemplateId = dialog.TemplateSettings.ModifyTemporaryAddedRecord_TemplateId, ModifyTemporaryEditedRecord_TemplateId = dialog.TemplateSettings.ModifyTemporaryEditedRecord_TemplateId, RemoveTemporaryAddedRecord_TemplateId = dialog.TemplateSettings.RemoveTemporaryAddedRecord_TemplateId, RowEditTemplateControlId = dialog.TemplateSettings.RowEditTemplateControlId, RowEditTemplateControlPropertyName = dialog.TemplateSettings.RowEditTemplateControlPropertyName, RowEditTemplateUseQuotes = dialog.TemplateSettings.RowEditTemplateUseQuotes, TemplateId = dialog.TemplateSettings.TemplateId, UndeleteTemporaryDeletedRecord_TemplateId = dialog.TemplateSettings.UndeleteTemporaryDeletedRecord_TemplateId };
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}

